var cloneDeep = require('lodash.clonedeep');

/**
 * 是否不为空
 * 
 * @param {String | Number | Object | Array } value 
 * @return Boolean  true-不为空 false-为空
 */
export function isNotEmpty (value) {
  let flag = (value !== null && value !== undefined && value !== '' && value !== 'undefined')
  if (flag) {
    const valueType = value.constructor
    // 如果是对象
    if (valueType === Object) {
      flag = Object.keys(value).length > 0
    }
    // 如果是数组
    if (valueType === Array) {
      flag = value.length > 0
    }
  }
  return flag
}

export function isEmpty (value) {
  return !isNotEmpty(value)
}

export function deepClone(source) {
  return cloneDeep(source)
}

/**
 * 获取随机数
 * 
 * @param {*} minNum 最小
 * @param {*} maxNum 最大
 * return value
 */
export function getRandomNum (minNum, maxNum) {
    var resultNum = 0;
    switch (arguments.length) {
      case 1: 
        resultNum = parseInt(Math.random() * minNum + 1, 10)
        break;
      case 2:
        resultNum = parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); 
        break;
      default:
        resultNum = Math.round(Math.random() * 100);
        break;
    }
    return resultNum;
}

export function getUUID() {
  let s = [];
  let hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  let uuid = s.join("");
  return uuid
}

export function getDomUUID() {
  let s = [];
  let hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
  // s[8] = s[13] = s[18] = s[23] = "-";

  let uuid = s.join("");
  return uuid
}

const utils = {
  isNotEmpty, isEmpty, deepClone, getUUID,
  getRandomNum, getDomUUID
}

export default utils
